package com.dgut.springsecurity.Details;

import com.dgut.adminmanager.entity.Administrator;
import com.dgut.adminmanager.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

@Component
public class AdminDetailService implements UserDetailsService {

    private boolean reachLimit=false;
    @Autowired
    private AdminService adminService;

    public boolean isReachLimit() {
        return reachLimit;
    }

    public void setReachLimit(boolean reachLimit) {
        this.reachLimit = reachLimit;
    }

    @Override
    public UserDetails loadUserByUsername(String account) throws UsernameNotFoundException {

        Administrator administrator= adminService.findAdministratorByAccount(account);
        if (administrator == null)
            return new AdminDetail(new Administrator(), AuthorityUtils.createAuthorityList("noADMIN"));
        else
            return new AdminDetail(administrator, AuthorityUtils.createAuthorityList(administrator.getPower()));

    }
}
